package com.school_admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.school_admin.entity.Operation;
import com.school_admin.mapper.OperationMapper;
import com.school_admin.service.IOperationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
import org.apache.mahout.cf.taste.impl.model.GenericDataModel;
import org.apache.mahout.cf.taste.impl.model.GenericPreference;
import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.UncenteredCosineSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.PreferenceArray;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2024-03-18
 */
@Service
public class OperationServiceImpl extends ServiceImpl<OperationMapper, Operation> implements IOperationService {
    private final OperationMapper operationMapper;

    @Autowired
    public OperationServiceImpl(OperationMapper operationMapper) {
        this.operationMapper = operationMapper;
    }

    public List<Operation> getUserNum(String uid) {
        LambdaQueryWrapper<Operation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Operation::getUid, uid);
        return operationMapper.selectList(wrapper);
    }

    // TODO 记录用户操作
    public void recordOperation(Operation operation) {
        operationMapper.insert(operation);
        System.out.println("记录用户操作");
    }

//    public List<Long> recommend(Integer uid) throws TasteException {
//        List<Operation> userList = operationMapper.getAllUserPreference();
//        //创建数据模型
//        DataModel dataModel = this.createDataModel(userList);
//        //获取用户相似程度
//        UserSimilarity similarity = new UncenteredCosineSimilarity(dataModel);
//        //获取用户邻居
//        UserNeighborhood userNeighborhood = new NearestNUserNeighborhood(2, similarity, dataModel);
//        System.out.println("相邻用户 = " + Arrays.toString(userNeighborhood.getUserNeighborhood(uid)));
//        //构建推荐器
//        Recommender recommender = new GenericUserBasedRecommender(dataModel, userNeighborhood, similarity);
//        //推荐2个
//        List<RecommendedItem> recommendedItems = recommender.recommend(uid, 3);
//        System.out.println("recommendedItems = " + recommendedItems);
//        return recommendedItems.stream().map(RecommendedItem::getItemID).collect(Collectors.toList());
//    }
//    private GenericDataModel createDataModel(List<Operation> operations) {
//        FastByIDMap<PreferenceArray> fastByIdMap = new FastByIDMap<>();
//        Map<Integer, List<Operation>> map = operations.stream().collect(Collectors.groupingBy(Operation::getUid));
//        Collection<List<Operation>> list = map.values();
//        for(List<Operation> userPreferences : list){
//            GenericPreference[] array = new GenericPreference[userPreferences.size()];
//            for(int i = 0; i < userPreferences.size(); i++){
//                Operation userPreference = userPreferences.get(i);
//                GenericPreference item = new GenericPreference(userPreference.getUid(), userPreference.getPostId(), userPreference.getValue());
//                array[i] = item;
//            }
//            fastByIdMap.put(array[0].getUserID(), new GenericUserPreferenceArray(Arrays.asList(array)));
//        }
//        return new GenericDataModel(fastByIdMap);
//    }
}
